package com.ruoyi.business.domain;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.bizsys.domain.LoanOrder;
import com.ruoyi.bizsys.domain.LoanOrderErrorLog;
import com.ruoyi.common.exception.BusinessException;
import io.swagger.annotations.ApiModel;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.validation.constraints.NotBlank;
import java.math.BigDecimal;
import java.util.Date;

/**
 * @Author: Jamnsin
 * @Date: 2021/3/10 3:23 下午
 */
@Data
@NoArgsConstructor
@ApiModel("贷款请求实体")
public class LoanReq {

    @NotBlank(message = "借款流水号不能为空")
    private String loanSerialNo;

    @NotBlank(message = "请输入交易密码")
    private String transPwd;

    @NotBlank(message = "产品编码不能为空")
    private String productCode;

    @NotBlank(message = "贷款期数不能为空")
    private Integer periodNum;

    @NotBlank(message = "期数单位不能为空")
    private String unit;

    @NotBlank(message = "贷款金额不能为空")
    private BigDecimal loanAmt;

    @NotBlank(message = "放款银行账号不能为空")
    private String accountNumber;

    // @NotBlank(message = "银行编码不能为空")
    private String bankCode;

    @NotBlank(message = "贷款用途不能为空")
    private String loanPurpose;

    private String loanPurposeOther;

    private JSONObject deviceInfo;

    /**
     * LoanReq 生成 LoanOrder
     *
     * @return
     */
    public LoanOrder generateLoanOrder() {
        LoanOrder loanOrder = new LoanOrder();
        loanOrder.setAccountNumber(this.getAccountNumber());
        loanOrder.setProductCode(this.getProductCode());
        loanOrder.setLoanAmt(this.getLoanAmt());
        loanOrder.setLoanPurpose(this.getLoanPurpose());
        loanOrder.setLoanPurposeOther(this.getLoanPurposeOther());
        loanOrder.setLoanSerialNo(this.getLoanSerialNo());
        return loanOrder;
    }

    /**
     * LoanReq 转LoanOrderErrorLog
     *
     * @param custNo
     * @param e
     * @return
     */
    public LoanOrderErrorLog parseLoanErrLog(String custNo, Exception e) {
        LoanOrderErrorLog loanOrderErrorLog = new LoanOrderErrorLog();
        loanOrderErrorLog.setCustNo(custNo);
        loanOrderErrorLog.setAccountNumber(getAccountNumber());
        loanOrderErrorLog.setProductCode(getProductCode());
        loanOrderErrorLog.setLoanAmt(getLoanAmt());
        loanOrderErrorLog.setLoanPurpose(getLoanPurpose());
        loanOrderErrorLog.setLoanPurposeOther(getLoanPurposeOther());
        loanOrderErrorLog.setLoanSerialNo(getLoanSerialNo());
        loanOrderErrorLog.setCreateTime(new Date());
        loanOrderErrorLog.setErrMsg(e.getMessage());
        if (e instanceof BusinessException) {
            BusinessException be = (BusinessException) e;
            loanOrderErrorLog.setErrCode(be.getCode() == null ? 0 : Long.valueOf(be.getCode()));
            loanOrderErrorLog.setErrDetail(be.getDetails());
        }
        return loanOrderErrorLog;
    }
}
